<?php
include_once 'fun.php';
include_once 'islogin.php';
$_SESSION['isadmin']=isset($_SESSION['isadmin'])?$_SESSION['isadmin']:'';
if ($_SESSION['isadmin']!=1&&$_SESSION['isadmin']!=-1) {
    header('Refresh:1;url=shop.php');
    die("请以库存管理员身份登录");
}
    //var_dump($_FILES);
    $Name = trim($_POST['Name']);
    $stockin = ($_POST['stockin']!='')?$_POST['stockin']:0;
    $price =($_POST['price']!='')?trim($_POST['price']):0;
   // $pic='pics/default.PNG';
    if(empty($Name))
    {
    header('Refresh:0;url=stock.php');
    die("商品名不能为空");
    }
   else if(empty($stockin)&&(!empty($price)))
    {
        header('Refresh:0;url=test.php');
        die("进货量不能为空");
    }
    else if((!empty($stockin))&&(empty($price)))
    {
        header('Refresh:0;url=test.php');
        die("进价不能为空");
    }
    else
    {
        $con=connect();
        $sql = "select count(*) from product where Name like '$Name' ";
        $num=query($sql,$con)->fetch_row();
        if($num[0]==0)
        {
        $sql = "insert into product (Name,stocknum,costoverall)values ('{$Name}','{$stockin}','{$price}')";
        query($sql,$con);
    
        }
        else {
            $sql = "update product set stocknum=stocknum+{$stockin},costoverall= costoverall+{$price} where Name like '{$Name}'";
            query($sql,$con);
            
        }
        echo "<script>
        location.href='stock.php';
</script>";
    
    }
    
    
?>